#include "include.h"
#include "func.h"
#include "func_music.h"
#include "func_exspiflash_music.h"
#include "api_spiflash_ex.h"

#if (EX_SPIFLASH_SUPPORT)
#define MAX_TIMEOUT_TIME 30
#define MAX_MICIN_TIMEOUT_TIME 5*60

#define SONG_INT_NUM 9
#define MIC_TTS_NUM 12

exspifalsh_msc_t exspi_msc AT(.buf.func.music);

AT(.text.func.music.table)
const u8 song_table[3][3]=
{
	{3,8,4},
	{2,1,1},//lock
	{5,6,7}
};
AT(.text.func.music.table)
const u8 max_song_table[3]=
{
	3,2,3
};
AT(.text.func.music.table)
const u16 delay_info_table[3][3]=
{
	{500,500,500},
	{500,500,500},
	{0,0,0},
};
AT(.text.func.music.table)
const song_time_play_t song1_time[]=
{
	{0,LED_SEQUENCE_5},		//1
	{252,LED_SEQUENCE_3},
	{328,LED_DOWNWARD_UPWARD},
	{400,LED_SEQUENCE_3},
	{1297,LED_BACK_AND_FORTH},
	{1690,LED_ON_AND_DOWNWARD},
	{1704,LED_UPWARD_AND_FLASH},
	{1821,LED_SEQUENCE_3},
	{2855,LED_ALL_FLASH},
	{3084,LED_SEQUENCE_3},//10
	{3200,LED_ALL_ON},
	{3320,LED_DOWNWARD_UPWARD},
	{3410,LED_ALL_FLASH},
	{3637,LED_DOWNWARD_OFF},
	{3728,LED_SEQUENCE_3},
	{3849,LED_ALL_FLASH},
	{3988,LED_ALL_FLASH},
	{4273,LED_SEQUENCE_3},
	{4744,LED_ON_AND_DOWNWARD},
	{4799,LED_UPWARD_AND_FLASH},//20
	{5246,LED_BACK_AND_FORTH},
	{5624,LED_SEQUENCE_3},
	{6077,LED_ON_AND_DOWNWARD},
	{6103,LED_UPWARD_AND_FLASH},
	{6714,LED_ON_AND_DOWNWARD},
	{6739,LED_ALTERNATE_FLASH},
	{7024,LED_ALL_FLASH},
	{7576,LED_ON_AND_DOWNWARD},
	
};
AT(.text.func.music.table)
const song_time_play_t song2_time[]=
{
	{0,LED_SEQUENCE_3},		//1
	{287,LED_BACK_AND_FORTH},
	{445,LED_SEQUENCE_3},
	{991,LED_ALL_FLASH},
	{1011,LED_ALL_ON},
	{1031,LED_SEQUENCE_3},
	{1813,LED_BACK_AND_FORTH},
	{2076,LED_SEQUENCE_3},
	{3518,LED_BACK_AND_FORTH},
	{3809,LED_SEQUENCE_3},//10
	{4635,LED_BACK_AND_FORTH},
	{4907,LED_SEQUENCE_3},
	{5864,LED_ON_AND_DOWNWARD},
	{5916,LED_ALL_ON},
	{5980,LED_SEQUENCE_11},
	{6020,LED_ON_AND_DOWNWARD},
	{6082,LED_BACK_AND_FORTH},
	{6483,LED_SEQUENCE_3},//10
	{7123,LED_ALL_ON},
	{7587,LED_SEQUENCE_11},
	{7607,LED_ALL_ON},
	{7621,LED_ON_AND_DOWNWARD},
};
u32 song_ticks = 0;
u16 play_time=0;


bool exspiflash_init(void)
{
    printf("exspiflash_init\n");
    spiflash1_init();
    if (!is_exspiflash_online()) {
        return false;
    }
    return true;
}
AT(.text.func.music)
void set_song_play_num(void)
{

	u8 song_index=exspi_msc.song_info[exspi_msc.song_mode].song_index;
	u8 led_mode=LED_SEQUENCE_3;
	printf("music song mode:%d->song index:%d\n",exspi_msc.song_mode,song_index);
	if(exspi_msc.mic_tts)
	{
		exspi_msc.cur_num=MIC_TTS_NUM;
		exspi_msc.delay_ticks=0;
		if(sys_cb.mic_onoff)
		{
			led_mode=LED_SEQUENCE_7;
		}
		else
		{
			led_mode=LED_SEQUENCE_8;
		}
	}
	else if(exspi_msc.song_int_flag)
	{
		exspi_msc.cur_num=SONG_INT_NUM;
		led_mode=LED_SEQUENCE_4;
	}
	
	else
	{
		exspi_msc.cur_num= song_table[exspi_msc.song_mode][song_index];
		exspi_msc.mic_light_cnt=0;
		if(exspi_msc.song_mode==1)
		{
			#if 0
			if(song_index==0)
			{
				led_mode=song1_time[0].light_mode;
				exspi_msc.time_offset=1;
			}
			else if(song_index==1)
			{
				led_mode=song2_time[0].light_mode;
				exspi_msc.time_offset=1;
			}
			else
			{
				led_mode=LED_SEQUENCE_3;
			}
			#endif
			led_mode=LED_SEQUENCE_3;
			play_time=0;
		}
		else if(exspi_msc.song_mode==2&&song_index==1)
		{
			led_mode=LED_SEQUENCE_4;
		}
		else
		{
			led_mode=LED_SEQUENCE_9;
		}
		if(exspi_msc.song_mode==0)
		{
			exspi_msc.pre_song=1;
		}
		else
		{
			exspi_msc.pre_song=0;
		}
	}

	exspi_msc.pause=false;
	amp_speaker_unmute();
	exspifalsh_music_num_kick(exspi_msc.cur_num);
	bsp_set_led_mode(led_mode);

}
AT(.text.func.music)
void func_check_song_play(void)
{
	if (get_music_dec_sta() == MUSIC_STOP)
	{
		u16 delay_time;
		if(exspi_msc.pause)
		{
			return;
		}
		if(exspi_msc.mic_tts)
		{
			if(exspi_msc.ticks_start==0)
			{
				exspi_msc.delay_ticks=0;
				exspi_msc.ticks_start=1;
				exspi_msc.delay_ticks=tick_get();
			}
			if(tick_check_expire(exspi_msc.delay_ticks,200))
			{
				exspi_msc.delay_ticks=tick_get();
				printf("tick end:%d\n",delay_time);
			}
			else
			{
				//printf("tick !!\n");
				return;
			}
			exspi_msc.ticks_start=0;
			exspi_msc.pause=true;
			exspi_msc.timeout_cnt=0;
			exspi_msc.mic_tts=0;			
			if(!sys_cb.mic_onoff)
			{
				amp_speaker_mute();
			}
			bsp_set_led_mode(LED_SEQUENCE_2);
			return;
		}
		else if(exspi_msc.song_int_flag)
		{
			exspi_msc.pause=true;
			exspi_msc.timeout_cnt=0;
			bsp_set_led_mode(LED_SEQUENCE_2);
			return;
		}
		if(exspi_msc.song_mode==3)
		{
			exspi_msc.pause=true;
			exspi_msc.timeout_cnt=0;
			
			printf("last func:%d\n",func_cb.last);
			if(sys_cb.bt_connected)
			{
				music_control(MUSIC_MSG_STOP);
				exspi_msc.song_info[1].song_index++;
				if(exspi_msc.song_info[1].song_index>=max_song_table[1])
				{
					exspi_msc.song_info[1].song_index=0;
				}
				func_cb.sta = FUNC_BT;
			}
			else
			{
				
				bsp_set_led_mode(LED_SEQUENCE_2);
			}
			return;
		}
		u8 song_index=exspi_msc.song_info[exspi_msc.song_mode].song_index;

		delay_time=delay_info_table[exspi_msc.song_mode][song_index];
		if(delay_time!=0)
		{

			if(tick_check_expire(exspi_msc.delay_ticks,delay_time))
			{
				exspi_msc.delay_ticks=tick_get();
				if(!exspi_msc.ticks_start)
				{
					printf("tick start:%d\n",delay_time);
					exspi_msc.ticks_start=1;
					return;
				}
			}
			else
			{
				return;
			}
			exspi_msc.ticks_start=0;
			printf("tick end!!\n");
		}
		printf("song mode:%d\n",exspi_msc.song_mode);
		if(exspi_msc.pre_song)
		{
			exspi_msc.pre_song=0;
		}
		switch(exspi_msc.song_mode)
		{
			case 0:
				#if 0
				exspi_msc.song_info[0].song_index++;
				if(exspi_msc.song_info[0].song_index>=max_song_table[0])
				{
					exspi_msc.song_info[0].song_index=0;
				}
				#endif
				exspi_msc.song_mode=1;
				break;
			case 1:
				if(!sys_cb.play_mode)
				{
					exspi_msc.pause=true;
					exspi_msc.timeout_cnt=0;
					if(sys_cb.bt_connected)
					{
						music_control(MUSIC_MSG_STOP);
						func_cb.sta = FUNC_BT;
					}
					else
					{
						bsp_set_led_mode(LED_SEQUENCE_2);
					}
					return;
				}
				#if 0
				exspi_msc.song_info[1].song_index++;
				if(exspi_msc.song_info[1].song_index>=max_song_table[1])
				{
					exspi_msc.song_info[1].song_index=0;
				}
				#endif
				#if FUNC_NO_SPEECH
				exspi_msc.song_mode=3;
				return;
				#else
				exspi_msc.song_mode=2;
				#endif
				break;
			case 2:
				exspi_msc.song_info[2].song_index++;
				if(exspi_msc.song_info[2].song_index>=max_song_table[2])
				{
					exspi_msc.song_info[2].song_index=0;
				}
				exspi_msc.song_mode=3;
				return;
				break;
		}
		set_song_play_num();

	}
}
AT(.text.func.music)
void func_switch_song(void)
{
	printf("song_mode:%d\n",exspi_msc.song_mode);
	#if 0
	sys_cb.play_mode=0;
	music_control(MUSIC_MSG_STOP);
	exspi_msc.song_int_flag=0;
	exspi_msc.song_info[1].song_index++;
	if(exspi_msc.song_info[1].song_index>=max_song_table[1])
	{
		exspi_msc.song_info[1].song_index=0;
	}
	exspi_msc.song_mode=1;
	#else
	if(sys_cb.play_mode==1)
	{
		music_control(MUSIC_MSG_STOP);
		exspi_msc.song_int_flag=0;
		exspi_msc.song_info[1].song_index++;
		if(exspi_msc.song_info[1].song_index>=max_song_table[1])
		{
			exspi_msc.song_info[1].song_index=0;
		}
	}
	else
	{
		if(exspi_msc.song_int_flag||(get_music_dec_sta() != MUSIC_PLAYING))
		{
			music_control(MUSIC_MSG_STOP);
			exspi_msc.song_int_flag=0;
			exspi_msc.song_info[1].song_index++;
			if(exspi_msc.song_info[1].song_index>=max_song_table[1])
			{
				exspi_msc.song_info[1].song_index=0;
			}
		}
		else
		{
			music_control(MUSIC_MSG_STOP);
			exspi_msc.song_int_flag=1;
		}
		
	}
	sys_cb.play_mode=0;
	exspi_msc.mic_tts=0;
	exspi_msc.song_mode=1;
	#endif
	set_song_play_num();
}
AT(.text.func.music)
void func_switch_play_pause(void)
{
	if(sys_cb.play_mode==0)
	{
		music_control(MUSIC_MSG_STOP);
		exspi_msc.pre_song=1;
	}
	if ((get_music_dec_sta() == MUSIC_PLAYING)&&(exspi_msc.song_int_flag==0))
	{
		music_control(MUSIC_MSG_STOP);
		exspi_msc.pause=true;
		exspi_msc.timeout_cnt=0;

		printf("song_mode:%d\n",exspi_msc.song_mode);
		switch(exspi_msc.song_mode)
		{
			case 0:
				#if 0
				printf("song_index:%d\n",exspi_msc.song_info[0].song_index);
				if(exspi_msc.song_info[0].song_index>(max_song_table[0]-1))
				{
					exspi_msc.song_info[0].song_index=0;
				}
				else
				{
					exspi_msc.song_info[0].song_index++;
				}
				printf("song_index 11:%d\n",exspi_msc.song_info[0].song_index);
				#endif
				exspi_msc.song_info[1].song_index++;
				if(exspi_msc.song_info[1].song_index>=max_song_table[1])
				{
					exspi_msc.song_info[1].song_index=0;
				}
				break;
			case 1:				
				break;
			case 2:
				exspi_msc.song_info[2].song_index++;

				if(exspi_msc.song_info[2].song_index>=max_song_table[2])
				{
					exspi_msc.song_info[2].song_index=0;
				}
				break;
			default:
				break;
		}
		if(!sys_cb.play_mode)
		{
			exspi_msc.song_int_flag=0;
			#if FUNC_NO_SPEECH
			exspi_msc.song_mode=1;
			#else
			exspi_msc.song_mode=0;
			#endif
			//set_song_play_num();
		}
		else
		{
			if(exspi_msc.song_mode==0)
			{
				exspi_msc.song_mode=1;
				exspi_msc.pre_song=0;
			}
			else
			{
				exspi_msc.song_int_flag=1;
				exspi_msc.pre_song=0;
				bsp_set_led_mode(LED_SEQUENCE_2);
			}
		}
		set_song_play_num();

	}
	else
	{
		printf("song int:%d\n",exspi_msc.song_int_flag);
		if(exspi_msc.song_int_flag||exspi_msc.song_mode==3)
		{
			exspi_msc.pre_song=1;
		}
		music_control(MUSIC_MSG_STOP);
		exspi_msc.song_int_flag=0;
		#if FUNC_NO_SPEECH
		exspi_msc.song_mode=1;
		#else
		exspi_msc.song_mode=0;
		#endif

		if(exspi_msc.song_mode==0&&exspi_msc.pre_song)
		{
			exspi_msc.song_info[0].song_index++;
			if(exspi_msc.song_info[0].song_index>(max_song_table[0]-1))
			{
				exspi_msc.song_info[0].song_index=0;
			}
			exspi_msc.song_info[1].song_index++;
			if(exspi_msc.song_info[1].song_index>(max_song_table[1]-1))
			{
				exspi_msc.song_info[1].song_index=0;
			}
		}
		printf("song mode:%d->song index:%d\n",exspi_msc.song_mode,exspi_msc.song_info[0].song_index);
		set_song_play_num();
	}
	sys_cb.play_mode=1;
}

AT(.text.func.music)
void func_switch_mic_onoff(u8 onoff)
{
	printf("song mode:%d\n",exspi_msc.song_mode);
	exspi_msc.mic_light=0;
	if(onoff)
	{
		sys_cb.mic_onoff=1;
		exspi_msc.timeout_cnt=0;
		exspi_msc.max_timeout_cnt=MAX_MICIN_TIMEOUT_TIME;
		if (get_music_dec_sta() == MUSIC_PLAYING&&(exspi_msc.song_mode==1))
		{
			exspi_msc.mic_light=1;
			bsp_set_led_mode(LED_SEQUENCE_3);			
			
		}
		else
		{
			bsp_set_led_mode(LED_SEQUENCE_6);
		}
		if(get_music_dec_sta() != MUSIC_PLAYING)
		{
			amp_speaker_mute();
		}
		else
		{
			if(exspi_msc.mic_tts)
			{
				music_control(MUSIC_MSG_STOP);
				exspi_msc.mic_tts=0;
			}
		}
		bsp_karaok_init(AUDIO_PATH_KARAOK, func_cb.sta);
		if(get_music_dec_sta() != MUSIC_PLAYING)
		{
			delay_ms(300);
			amp_speaker_unmute();
			exspi_msc.mic_tts=1;
			set_song_play_num();
		}
	}
	else
	{
		sys_cb.mic_onoff=0;
		exspi_msc.timeout_cnt=0;
		exspi_msc.max_timeout_cnt=MAX_TIMEOUT_TIME;
		if(get_music_dec_sta() != MUSIC_PLAYING)
		{
			amp_speaker_mute();
			delay_ms(100);
		}
		else
		{
			if(exspi_msc.mic_tts)
			{
				music_control(MUSIC_MSG_STOP);
				exspi_msc.mic_tts=0;
			}
		}
		bsp_karaok_exit(AUDIO_PATH_KARAOK);
		if (get_music_dec_sta() == MUSIC_PLAYING)
		{
			if(exspi_msc.song_mode==1)
			{
				bsp_set_led_mode(LED_SEQUENCE_3);
			}
			else
			{
				bsp_set_led_mode(LED_SEQUENCE_4);
			}

		}
		else if(get_music_dec_sta() != MUSIC_PLAYING)
		{			
			delay_ms(300);
			amp_speaker_unmute();
			exspi_msc.mic_tts=1;
			set_song_play_num();
		}
	}
}

AT(.text.func.music)
void func_song_light_sync_deal(void)
{

	u16 song_play_time=0;
	u8 light_mode=0;
	u8 song_index=exspi_msc.song_info[exspi_msc.song_mode].song_index;
	if(sys_cb.cur_mode!=HOME_MODE)
	{
		return;
	}
	if(exspi_msc.song_mode!=1)
	{
		return;
	}	
	if(exspi_msc.pause)
	{
		return;
	}
	if(tick_check_expire(song_ticks,10))
	{
		song_ticks = tick_get();
		
		//printf("cur_num:%d\n",exspi_msc.cur_num);
		if(song_index==0)
		{
			#if 0
			msc_breakpiont_t song_brkpt;
			u32 cur_time1 = music_get_cur_time();
			music_get_breakpiont(&song_brkpt);			
			printf("time:%d->frame_count:0x%x->file_ptr:0x%x\n",cur_time1,song_brkpt.frame_count,song_brkpt.file_ptr);
			#endif
			song_play_time=song1_time[exspi_msc.time_offset].time;
			light_mode=song1_time[exspi_msc.time_offset].light_mode;
			
		}
		else if(song_index==1)
		{
			
			song_play_time=song2_time[exspi_msc.time_offset].time;
			light_mode=song2_time[exspi_msc.time_offset].light_mode;
			
		}
		if(exspi_msc.mic_light)
		{
			exspi_msc.mic_light_cnt++;
			if(exspi_msc.mic_light_cnt>=100)
			{
				exspi_msc.mic_light_cnt=0;
				exspi_msc.mic_light=0;
			}
		}
		if(play_time==song_play_time)
		{
			printf("play time:%d->light:%d\n",song_play_time,light_mode);
			u32 cur_time1 = music_get_cur_time();
			if(play_time!=(cur_time1/10))
			{
				play_time=cur_time1*10;
			}
			if(!exspi_msc.mic_light)
			{
				bsp_set_led_mode(light_mode);	
			}					
			exspi_msc.time_offset++;
		}
		play_time++;
	}
}

AT(.text.func.music)
void func_exspifalsh_music_process(void)
{
    func_process();
	func_check_song_play();
	//func_song_light_sync_deal();
}

AT(.text.func.music)
bool exspifalsh_music_image_check(void)
{
    if (is_exspiflash_online()) {
        exspi_msc.total_num = exspifalsh_music_get_file_total();
		printf("exspi_msc.total_num = %d\n",exspi_msc.total_num);
        if (exspi_msc.total_num > 0) {
            return true;
        }
    }
    return false;
}

AT(.text.func.music)
void func_check_timeout(void)
{
	#if 0
	if(sys_cb.mic_onoff)
	{
		exspi_msc.timeout_cnt=0;
		return;
	}
	#endif
	if(exspi_msc.pause)
	{
		exspi_msc.timeout_cnt++;
		printf("timeout cnt:%d",exspi_msc.timeout_cnt);
		amp_speaker_mute();
		if(exspi_msc.timeout_cnt>=exspi_msc.max_timeout_cnt)
		{
			exspi_msc.timeout_cnt=0;
			//exspi_msc.song_int_flag=0;
			music_control(MUSIC_MSG_STOP);
			sys_cb.pwrdwn_tone_en=0;
			func_cb.sta = FUNC_PWROFF;
		}
	}
}
AT(.text.func.music)
void func_lower_power_check(void)
{
	u8 mic_off;
	if(sys_cb.mic_onoff&&sys_cb.karaok_init)
	 {

	 	mic_off=bsp_get_mic_vol_offset();
		if(sys_cb.mic_vol_offset!=mic_off)
		{
			sys_cb.mic_vol_offset=mic_off;
			sys_cb.music_vol_offset=mic_off;
			bsp_karaok_set_mic_volume();
			bsp_change_volume(MUSIC_VOL-mic_off);
		}
		if ((sys_cb.vbat <=2700) ||(adc_cb.vrtc_val>=0x39f))
		 {
		 	sys_cb.vbat_low=1;
			sys_cb.mic_onoff=0;
			bsp_karaok_exit(AUDIO_PATH_KARAOK);
		 }
	 }
	 else
	{
		mic_off=bsp_get_mic_vol_offset();
		//printf("vol_offset:%d->offset:%d\n",mic_off,sys_cb.music_vol_offset);
		if(sys_cb.music_vol_offset!=mic_off)
		{
			sys_cb.music_vol_offset=mic_off;
			bsp_change_volume(MUSIC_VOL-mic_off-1);
		}

	}
}
AT(.text.func.music)
void func_song_info_init(void)
{
	//printf("first_into_song:%d\n",sys_cb.first_into_song);
	memset(&exspi_msc,0x0,sizeof(exspifalsh_msc_t));
}

AT(.text.func.music)
static void func_exspifalsh_music_enter(void)
{
    msg_queue_clear();
    if ((!exspifalsh_music_image_check())){
        func_cb.sta = FUNC_NULL;
        return;
    }

    func_cb.mp3_res_play = NULL;//func_exspifalsh_mp3_res_play;
    register_spi_read_function(spiflash1_read);
	amp_speaker_unmute();
    bsp_change_volume(0);
	#if SYS_KARAOK_EN
	if(sys_cb.mic_onoff)
	{
		music_control(MUSIC_MSG_STOP);
		bsp_karaok_init(AUDIO_PATH_KARAOK, func_cb.sta);
		delay_ms(10);
	}
	#endif
	bsp_change_volume(MUSIC_VOL);
	if(func_cb.last!=FUNC_BT)
	{
		func_song_info_init();
	}
    exspi_msc.cur_num = 1;            //默认从1首开始播放
    exspi_msc.pause = true;
	exspi_msc.pause_cnt=0;
	exspi_msc.song_int_flag=0;
	printf("play mode:%d\n",sys_cb.play_mode);

	if(sys_cb.play_mode>1)
	{
		sys_cb.play_mode=0;
	}
	#if FUNC_NO_SPEECH
	exspi_msc.song_mode=1;
	#else
	if(sys_cb.play_mode==1)
	{
		exspi_msc.song_mode=0;
	}
	else
	{
		exspi_msc.song_mode=1;
	}
	#endif

	exspi_msc.max_timeout_cnt=MAX_TIMEOUT_TIME;
	set_song_play_num();
}

AT(.text.func.music)
static void func_exspifalsh_music_exit(void)
{
    music_control(MUSIC_MSG_STOP);
	sys_cb.exspiflash_music_mode=0;
	if(sys_cb.mic_onoff)
	{
		 bsp_karaok_exit(AUDIO_PATH_KARAOK);
		 delay_ms(30);
	}
    register_spi_read_function(NULL);  //恢复内部SPI读接口.
    func_cb.last = FUNC_EXSPIFLASH_MUSIC;
}



AT(.text.func.music)
void func_exspifalsh_music(void)
{
    printf("%s\n", __func__);
    func_exspifalsh_music_enter();
    while (func_cb.sta == FUNC_EXSPIFLASH_MUSIC) {
        func_exspifalsh_music_process();
        func_exspifalsh_music_message(msg_dequeue());
    }
    func_exspifalsh_music_exit();
}
#endif

